#ifndef SERIALCOMMUNICATOR_THREAD_H
#define SERIALCOMMUNICATOR_THREAD_H

#include <QThread>

#include "qextserialport.h"
#include "autointerfile.h"

#define SERIAL_READ_GAP 100
#define SCFRIEND_READ_BUFSIZE (4096)
//#define READ_STRING_ARRAY_SIZE 10
enum Triger
{
	TRIGER_TRY_START, // 
    NEW_READ_STRING,
    TIME_ELAPSE,
    AFTER_WRITE
};

class CSerialThread : public QThread
{
    Q_OBJECT
public:
    CSerialThread();

    void run();
    bool writeDataAsynchro(QString str, bool isHex);// this function must be called outside.
    bool TrigerCmd(Triger tg, QString * pstr = NULL);

private:
    void setNewOptions();
    bool enableLogging(bool on, QString path, int lgtype);
    QString * convertBuffToString();
    void readData();
    void readSettings();

signals:
   // void explains();
    void AddStr(QString * str);
   void ShowExeCmd(cmdList * cmd);

public:
    /*      thread params       */
    volatile bool stopp_b;
    volatile bool write_b;
    volatile bool read_b;

    /*      IO params       */
    char m_buf[SCFRIEND_READ_BUFSIZE];
    int  bytesRead;
    //QString readstrarr[READ_STRING_ARRAY_SIZE];
    QByteArray writeByteArray;
    QString m_errorinfo;
    bool m_writeAble;
    bool m_HexOutPut;
    bool m_DatePrefix;
    bool m_TimePrefix;
    bool m_PrintUnchar;
    int m_hexBytes;
    cmdList * m_cmdlist;
	cmdList * m_curCmd;
	
    /*      port params     */
    QextSerialPort m_SerialPort;
    QString m_name;
    int baudrate;
    int dataBits;
    QString parity;
    QString stop;
    bool softwareHandshake;
    bool hardwareHandshake;


    /*      log params      */
    bool enableloging ;
    QFile m_logFile;
    QString logPath ;
    int logType ;

};

#endif // SERIALCOMMUNICATOR_THREAD_H
